package org.jeecg.modules.workflow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.message.entity.SysMessage;
import org.jeecg.modules.message.service.ISysMessageService;
import org.jeecg.modules.workflow.entity.WorkFlowBase;
import org.jeecg.modules.workflow.entity.WorkFlowBaseAssign;
import org.jeecg.modules.workflow.service.IWorkFlowBaseAssignService;
import org.jeecg.modules.workflow.service.IWorkFlowBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 工作流基础
 * @author: wufanxin
 * @date: 2021-06-11
 * @version: V1.0
 */
@Slf4j
@RestController
@RequestMapping("/sys/workflow")
@Api(tags = "工作流基础")
public class WorkFlowBaseController extends JeecgController<WorkFlowBase, IWorkFlowBaseService> {
	@Autowired
	private IWorkFlowBaseService workFlowBaseService;

	@Autowired
	private IWorkFlowBaseAssignService workFlowBaseAssignService;

	/**
	 * 工作流基础信息分页列表查询
	 *
	 * @param workFlowBase
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "工作流基础信息-分页列表查询", notes = "工作流基础信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryWorkFlowBaseList(WorkFlowBase workFlowBase, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
								   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		Page<WorkFlowBase> page = new Page<WorkFlowBase>(pageNo, pageSize);
		IPage<WorkFlowBase> pageList = workFlowBaseService.queryWorkFlowBaseList(page, workFlowBase);
        return Result.ok(pageList);
	}



	/**
	 * 新增工作流基础
	 *
	 * @param workFlowBase
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "工作流基础信息-新增工作流基础", notes = "工作流基础信息-新增工作流基础")
	@PostMapping(value = "/addWorkFlowBase")
	public Result<?> addWorkFlowBase(@RequestBody WorkFlowBase workFlowBase, HttpServletRequest req) {

		String username = JwtUtil.getUserNameByToken(req);
		workFlowBase.setCreateBy(username);
		workFlowBase.setUpdateBy(username);
		workFlowBaseService.addWorkFlowBase(workFlowBase);
		return Result.ok();
	}

	/**
	 * 删除工作流基础
	 *
	 * @param workFlowBase
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "工作流基础信息-删除工作流基础", notes = "工作流基础信息-删除工作流基础")
	@GetMapping(value = "/deleteWorkFlowBase")
	public Result<?> deleteWorkFlowBase(WorkFlowBase workFlowBase, HttpServletRequest req) {
		QueryWrapper<WorkFlowBase> wfquery = new QueryWrapper<>();
		wfquery.eq("workflow_id",workFlowBase.getWorkflowId());
		workFlowBaseService.remove(wfquery);

		QueryWrapper<WorkFlowBaseAssign> wfaquery = new QueryWrapper<>();
		wfaquery.eq("workflow_id",workFlowBase.getWorkflowId());
		workFlowBaseAssignService.remove(wfaquery);
		return Result.ok();
	}


}
